#ifndef __MUI_TYPEMAP_I
#define __MUI_TYPEMAP_I

%include <exception.i>

%typemap(in) const WCHAR* {
    if (PyUnicode_Check($input))
        $1 = (WCHAR*) PyUnicode_AsUnicode($input);
    else
        SWIG_exception(SWIG_TypeError, "Unicode string expected");
}

%typemap(in) (const WCHAR* str, UINT len) {
	if (PyUnicode_Check($input))
        $1 = (WCHAR*) PyUnicode_AsUnicode($input);
	else
        SWIG_exception(SWIG_TypeError, "Unicode string expected");
    $2 = PyUnicode_GetSize($input);       /* int len   */
}

%typemap(in) WCHAR* = const WCHAR *;
%typemap(typecheck) const WCHAR* {
	$1 = PyUnicode_Check($input) ? 1 : 0;
}
%typemap(typecheck) WCHAR* = const WCHAR*; 

%typemap(in) LPCWSTR = const WCHAR *;
%typemap(typecheck) LPCWSTR = const WCHAR*; 

%typemap(in, numinputs=0) WCHAR* OUTPUT_len512 (WCHAR tmp[512])
{
	memset(tmp, 0, sizeof(WCHAR)*512);
	$1 = tmp;
	
}

%typemap(in, numinputs=0) (WCHAR* OUTPUT_len512, int nOutMaxCount) (WCHAR tmp[512])
{
	memset(tmp, 0, sizeof(WCHAR)*512);
	$1 = tmp;
	$2 = 512;
}

%typemap(argout) WCHAR* OUTPUT_len512 (PyObject* o1, PyObject* o2, PyObject* o3){
   o1 = PyUnicode_FromWideChar($1, 512);
   if ((!$result) || ($result == Py_None)) {
        $result = o1;
    } else {
        if (!PyTuple_Check($result)) {
            PyObject *o2 = $result;
            $result = PyTuple_New(1);
            PyTuple_SetItem($result,0,o2);
        }
        o3 = PyTuple_New(1);
        PyTuple_SetItem(o3,0,o1);
        o2 = $result;
        $result = PySequence_Concat(o2,o3);
        Py_DECREF(o2);
        Py_DECREF(o3);
    }
}

%typemap(in, numinputs=0) char* outStr_512 (char tmp[512])
{
	memset(tmp, 0, sizeof(char)*512);
	$1 = tmp;
}
%typemap(argout) char* OUTPUT_len512 (PyObject* o1, PyObject* o2, PyObject* o3){
   o1 = PyString_FromStringAndSize($1, 512);
   if ((!$result) || ($result == Py_None)) {
        $result = o1;
    } else {
        if (!PyTuple_Check($result)) {
            PyObject *o2 = $result;
            $result = PyTuple_New(1);
            PyTuple_SetItem($result,0,o2);
        }
        o3 = PyTuple_New(1);
        PyTuple_SetItem(o3,0,o1);
        o2 = $result;
        $result = PySequence_Concat(o2,o3);
        Py_DECREF(o2);
        Py_DECREF(o3);
    }
}


%typemap(in) HWND {
    if (PyInt_Check($input))
        $1 = ($1_type) PyInt_AsLong($input);
    else
        SWIG_exception(SWIG_TypeError, "Handle(int) expected");
}
%typemap(out) HWND {
    $result = PyInt_FromLong((long)$1);
   
}
%typemap(typecheck) HWND {
	$1 = PyInt_Check($input) ? 1 : 0;
}

%typemap(in, numinputs=0) Handle* OUTPUT ($*ltype temp)  
{ 
   temp = ($*ltype)0;
   $1 = &temp;
}

%typemap(argout) Handle* OUTPUT (PyObject* o1, PyObject* o2, PyObject* o3){
   o1 = PyInt_FromLong((int)*$1);
   if ((!$result) || ($result == Py_None)) {
        $result = o1;
    } else {
        if (!PyTuple_Check($result)) {
            PyObject *o2 = $result;
            $result = PyTuple_New(1);
            PyTuple_SetItem($result,0,o2);
        }
        o3 = PyTuple_New(1);
        PyTuple_SetItem(o3,0,o1);
        o2 = $result;
        $result = PySequence_Concat(o2,o3);
        Py_DECREF(o2);
        Py_DECREF(o3);
    }
}

%typemap(in) HANDLE = HWND;
%typemap(out) HANDLE  = HWND;
%typemap(typecheck) HANDLE = HWND;
%typemap(in) HINSTANCE = HWND;
%typemap(out) HINSTANCE  = HWND;
%typemap(typecheck) HINSTANCE = HWND;
%typemap(in) HMONITOR = HWND;
%typemap(out) HMONITOR = HWND;
   
%typemap(typecheck) HMONITOR = HWND;
%typemap(in) HMODULE = HWND;
%typemap(out) HMODULE = HWND;
%typemap(typecheck) HMODULE = HWND;

%typemap(in) HDC = HWND;
%typemap(out) HDC = HWND;
%typemap(typecheck) HDC = HWND;



%typemap(in) FLOAT value[ANY] (float temp[$1_dim0]) {
  int i = 0;
  if (!PySequence_Check($input)) {
    PyErr_SetString(PyExc_ValueError,"$1_name Expected a sequence");
    return NULL;
  }
  if (PySequence_Length($input) != $1_dim0) {
    PyErr_SetString(PyExc_ValueError,"Size mismatch. $1_name Expected $1_dim0 elements");
    return NULL;
  }
  for (i = 0; i < $1_dim0; i++) {
    PyObject *o = PySequence_GetItem($input,i);
    if (PyNumber_Check(o)) {
      temp[i] = (float) PyFloat_AsDouble(o);
    } else {
      PyErr_SetString(PyExc_ValueError,"$1_name Sequence elements must be numbers");      
      return NULL;
    }
  }
  $1 = temp;
}

%typemap(typecheck)  FLOAT value[ANY] (float temp[$1_dim0]) {
  int i = 0;
  if (!PySequence_Check($input)) {
    PyErr_SetString(PyExc_ValueError,"$1_name Expected a sequence");
    $1 = 0;
	return NULL;
  }
  if (PySequence_Length($input) != $1_dim0) {
    PyErr_SetString(PyExc_ValueError,"Size mismatch.$1_name Expected $1_dim0 elements");
    $1 = 0;
	return NULL;
  }
  $1 = 1;
}
%typemap(in) FLOAT color[ANY] = FLOAT value[ANY];
%typemap(typecheck)  FLOAT color[ANY] = FLOAT value[ANY];

%typemap(in) float color[ANY] = FLOAT value[ANY];
%typemap(typecheck)  float color[ANY] = FLOAT value[ANY];

%typemap(in) const float color[ANY] = FLOAT value[ANY];
%typemap(typecheck)  const float color[ANY] = FLOAT value[ANY];

%typemap(in) const float bordW[ANY] = FLOAT value[ANY];
%typemap(typecheck)  const float bordW[ANY] = FLOAT value[ANY];


%typemap(in) D2D1_POINT_2F (float temp[2]) {
  int i = 0;
  if (!PySequence_Check($input)) {
    PyErr_SetString(PyExc_ValueError,"$1_name Expected a sequence");
    return NULL;
  }
  if (PySequence_Length($input) != 2) {
    PyErr_SetString(PyExc_ValueError,"Size mismatch. $1_name Expected 2 elements");
    return NULL;
  }
  for (i = 0; i < 2; i++) {
    PyObject *o = PySequence_GetItem($input,i);
    if (PyNumber_Check(o)) {
      temp[i] = (float) PyFloat_AsDouble(o);
    } else {
      PyErr_SetString(PyExc_ValueError,"$1_name Sequence elements must be numbers");      
      return NULL;
    }
  }
  $1.x = temp[0];
  $1.y = temp[1];
}

%typemap(typecheck) D2D1_POINT_2F {
  int i = 0;
  if (!PySequence_Check($input)) {
    PyErr_SetString(PyExc_ValueError,"$1_name Expected a sequence");
    $1 = 0;
	return NULL;
  }
  if (PySequence_Length($input) != 2) {
    PyErr_SetString(PyExc_ValueError,"Size mismatch. $1_name Expected 2 elements");
    $1 = 0;
	return NULL;
  }
  $1 = 1;
}


%typemap(in) D2D1_RECT_F (float temp[4]) {
  int i = 0;
  if (!PySequence_Check($input)) {
    PyErr_SetString(PyExc_ValueError,"$1_name Expected a sequence");
    return NULL;
  }
  if (PySequence_Length($input) != 4) {
    PyErr_SetString(PyExc_ValueError,"Size mismatch. $1_name Expected 4 elements");
    return NULL;
  }
  for (i = 0; i < 4; i++) {
    PyObject *o = PySequence_GetItem($input,i);
    if (PyNumber_Check(o)) {
      temp[i] = (float) PyFloat_AsDouble(o);
    } else {
      PyErr_SetString(PyExc_ValueError,"$1_name Sequence elements must be numbers");      
      return NULL;
    }
  }
  $1.left = temp[0];
  $1.top = temp[1];
  $1.right = temp[2];
  $1.bottom = temp[3];
}


%typemap(typecheck) D2D1_RECT_F {
  int i = 0;
  if (!PySequence_Check($input)) {
    PyErr_SetString(PyExc_ValueError,"$1_name Expected a sequence");
    $1 = 0;
	return NULL;
  }
  if (PySequence_Length($input) != 4) {
    PyErr_SetString(PyExc_ValueError,"Size mismatch. $1_name Expected 4 elements");
    $1 = 0;
	return NULL;
  }
  $1 = 1;
}

%typemap(in) D2D_RECT_F = D2D1_RECT_F; 
%typemap(typecheck) D2D_RECT_F = D2D1_RECT_F; 


%typemap(in,numinputs=0) SWIGTYPE** OUTPUT ($*ltype temp)
{ 
   temp = ($*ltype)0;
   $1 = &temp; 
}

%typemap(argout) SWIGTYPE** OUTPUT (PyObject *o1, PyObject *o2, PyObject *o3){

    o1 = SWIG_NewPointerObj((void*)*$1 , $*1_descriptor, 0);
    if ((!$result) || ($result == Py_None)) {
        $result = o1;
    } else {
        if (!PyTuple_Check($result)) {
            PyObject *o2 = $result;
            $result = PyTuple_New(1);
            PyTuple_SetItem($result,0,o2);
        }
        o3 = PyTuple_New(1);
        PyTuple_SetItem(o3,0,o1);
        o2 = $result;
        $result = PySequence_Concat(o2,o3);
        Py_DECREF(o2);
        Py_DECREF(o3);
    }
}



#endif